<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

// 获取路由实例
const router = useRouter()

const username = ref('')
const password = ref('')
const errorMessage = ref('')

// 处理登录表单提交
const handleLogin = (e) => {
  e.preventDefault()
  
  // 简单的表单验证
  if (!username.value || !password.value) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  
  // 在实际项目中，这里会发送登录请求到后端
  console.log('登录请求:', { username: username.value, password: password.value })
  
  // 模拟登录成功
  alert('登录成功！欢迎回来，' + username.value)
  
  // 保存用户信息到localStorage
  localStorage.setItem('currentUser', JSON.stringify({
    id: Math.floor(Math.random() * 1000000), // 模拟用户ID
    username: username.value,
    email: username.value + '@example.com' // 模拟邮箱
  }))
  
  // 跳转到主页
  router.push('/onmyoji')
  
  // 清空表单和错误信息
  username.value = ''
  password.value = ''
  errorMessage.value = ''
}
</script>

<template>
  <div class="login-container">
    <button class="back-btn" @click="router.push('/onmyoji')">
      <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
        <path d="M19 12H5M5 12L12 5M5 12L12 19" stroke="#d4af37" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
      </svg>
    </button>
    <div class="login-form">
      <h2 class="login-title">阴阳师 - 登录</h2>
      
      <form @submit="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入您的用户名"
            class="form-input"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入您的密码"
            class="form-input"
            required
          />
        </div>
        
        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" />
            记住我
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <button type="submit" class="login-submit-btn">登录</button>
        
        <div class="register-link">
          还没有账号？请联系管理员获取账号
        </div>
        
        <div class="third-party-login">
          <p>其他登录方式</p>
          <div class="login-icons">
            <button type="button" class="third-party-btn wechat">微信</button>
            <button type="button" class="third-party-btn qq">QQ</button>
            <button type="button" class="third-party-btn phone">手机号</button>
          </div>
        </div>
      </form>
      
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
    </div>
  </div>
</template>

<style scoped>
/* 使用全局CSS变量 - 已在App.vue中定义 */
/* 页面特定变量 */
:root {
  --bg-dark: rgba(0, 0, 0, 0.9);
  --bg-light: rgba(255, 255, 255, 0.05);
}

.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.9));
  background-size: cover;
  background-position: center;
  font-family: 'Microsoft YaHei', 'STKaiti', serif;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at center, rgba(212, 175, 55, 0.2) 0%, transparent 70%);
  z-index: 0;
}

/* 返回按钮样式 */
.back-btn {
  position: absolute;
  top: 30px;
  left: 30px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid var(--gold-primary);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  z-index: 10;
}

.back-btn:hover {
  background: rgba(212, 175, 55, 0.2);
  transform: translateX(-5px);
  box-shadow: 0 0 20px rgba(212, 175, 55, 0.4);
}

.login-form {
  background: var(--bg-dark);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 0 30px rgba(212, 175, 55, 0.2);
  backdrop-filter: blur(10px);
}

.login-title {
  text-align: center;
  color: var(--gold-primary);
  font-size: 28px;
  margin-bottom: 30px;
  text-shadow: 0 0 10px rgba(212, 175, 55, 0.5);
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  color: #fff;
  margin-bottom: 8px;
  font-size: 14px;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  background: var(--bg-light);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: #fff;
  font-size: 14px;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: var(--gold-primary);
  box-shadow: 0 0 10px rgba(212, 175, 55, 0.3);
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.remember-me {
  display: flex;
  align-items: center;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}

.remember-me input {
  margin-right: 5px;
}

.forgot-password {
  color: var(--gold-primary);
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: var(--gold-secondary);
}

.login-submit-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-secondary));
  border: none;
  border-radius: 6px;
  color: #000;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 10px rgba(212, 175, 55, 0.3);
}

.login-submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(212, 175, 55, 0.5);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #fff;
  font-size: 14px;
}

.register-link a {
  color: var(--gold-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: var(--gold-secondary);
}

.third-party-login {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid var(--border-color);
}

.third-party-login p {
  text-align: center;
  color: #fff;
  margin-bottom: 15px;
  font-size: 14px;
}

.login-icons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.third-party-btn {
  padding: 10px 20px;
  background: var(--bg-light);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
}

.third-party-btn:hover {
  background: var(--border-color);
  transform: translateY(-2px);
}

.error-message {
  background: rgba(255, 0, 0, 0.2);
  border: 1px solid rgba(255, 0, 0, 0.3);
  border-radius: 6px;
  color: #ff6b6b;
  padding: 10px;
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-form {
    padding: 30px 20px;
    margin: 0 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
}
</style>